package com.wang.wx.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.wang.wx.common.Result;
import com.wang.wx.service.DataSplitterService;
import com.wang.wx.service.IOExcel;
import com.wang.wx.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * (WxUser)表控制层
 *
 * @author makejava
 * @since 2024-08-02 14:53:25
 */
@RestController
@RequestMapping("/admin/system/wxUser")
@Api(tags = "1、微信数据管理")
public class WxUserController extends ApiController {

    
    @Resource
    private DataSplitterService wxDataSplitterServiceImpl;
    
    @Resource
    private WxUserService wxUserService;
    @Resource
    private IOExcel ioExcel;
    @Value("${path.output}")
    private String outputFilePath;

    /**
     * 微信数据入库
     */
    @PostMapping("/upload")
    @ApiOperation(value = "1、输入文件路径，解析入库")
    public R dataReceipt(String filePath) throws IOException {
        wxDataSplitterServiceImpl.handleFile(filePath);
        return success("ok");
    }

    @ApiOperation(value = "2、创建索引")
    @GetMapping("/createIndex")
    public Result createIndex() {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 指定数据源名称
            hintManager.setDataSourceName("ds_wx");

            // 执行存储过程
            wxUserService.createIndexes();

            return Result.ok("创建微信索引成功");
        } catch (Exception e) {
            throw new RuntimeException("已存在相同的微信索引，不要重复创建", e);
        }
    }

    @ApiOperation(value = "3、删除索引")
    @GetMapping("/dropIndex")
    public Result dropIndex() {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 指定数据源名称
            hintManager.setDataSourceName("ds_wx");

            // 执行存储过程
            wxUserService.dropIndexes();

            return Result.ok("删除微信索引成功");
        } catch (Exception e) {
            throw new RuntimeException("已存在相同的微信索引，不要重复创建", e);
        }
    }
    
    

}

